package com.smasher.update.component.parse

import android.util.Log
import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException
import com.smasher.update.entity.BeanVersion
import kotlinx.serialization.InternalSerializationApi
import org.json.JSONObject

@OptIn(InternalSerializationApi::class)
class ParseImplSoftOut : ParseDelegate {

    private val gson = GsonBuilder().create()


    var version: BeanVersion? = null

    override fun parse(json: JSONObject) {
        try {
            version = gson.fromJson(json.toString(), BeanVersion::class.java)
        } catch (e: JsonSyntaxException) {
            Log.e(TAG, "Parse", e)
        }
    }

    override fun getUpdateTitle(): String {
        return version?.title ?: ""
    }

    override fun getUpdateContent(): String {
        return version?.content ?: ""
    }

    override fun getVersionCode(): Long {
        return version?.versionCode ?: 0L
    }

    override fun getDownLoadUrl(): String {
        return version?.androidLink ?: ""
    }

    override fun getType(): String {
        return version?.type ?: ""
    }

    override fun getUpdateTime(): Long {
        return version?.updateTime ?: 0L
    }

    companion object {
        const val TAG = "ParseImplSoftOut"
    }
}